package services

import (
	"context"
	"database/sql"
	db "dhuviewer/db_query"
	"dhuviewer/dto"
	"fmt"
	"time"

	"github.com/google/uuid"
	"github.com/labstack/gommon/log"
)

func CreateUser(uid uuid.UUID) error {
	res, err := queries.CreateUser(context.Background(), db.CreateUserParams{
		Uid:          uid,
		RegisterDate: sql.NullTime{Time: time.Now(), Valid: true},
	})
	if err != nil {
		log.Info("error:", err)
		return err
	}
	fmt.Println(res)
	return nil
}

func GetUser(uid uuid.UUID) (error, db.User) {
	userData, err := queries.GetUser(context.Background(), uid)
	if err != nil {
		log.Info("error:", err)
		return err, db.User{}
	}
	return err, userData
}

func UpdateUser(uid uuid.UUID, userdata *dto.User) error {
	err := queries.UpdateUser(context.Background(), db.UpdateUserParams{
		Uid:            uid,
		Note:           sql.NullString{String: userdata.Note, Valid: true},
		Gender:         sql.NullString{String: userdata.Gender, Valid: true},
		RegisterDate:   sql.NullTime{Time: userdata.RegisterDate, Valid: true},
		BirthDate:      sql.NullTime{Time: userdata.BirthDate, Valid: true},
		PerferedGender: sql.NullString{String: userdata.PerferedGender, Valid: true},
	})
	return err
}
